package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.Student;
import org.jeecg.modules.system.entity.Student;
import org.jeecg.modules.system.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 15579
 * 2019/7/3 10:24
 * 文件说明：
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生管理演示Student")
@Slf4j
public class DemoStudentController {
    @Autowired
    private IStudentService iStudentService;

    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public Result<IPage<Student>> queryPageList(Student student, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req){

        Result<IPage<Student>> result = new Result<IPage<Student>>();
        QueryWrapper<Student> queryWrapper = QueryGenerator.initQueryWrapper(student, req.getParameterMap());
        Page<Student> page=new Page<Student>(pageNo,pageSize);
        IPage<Student> pageList = iStudentService.page(page, queryWrapper);
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * @功能：删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @CacheEvict(value="dictCache", allEntries=true)
    public Result<Student> delete(@RequestParam(name="id",required=true) String id) {
        Result<Student> result = new Result<Student>();
        Student Student = iStudentService.getById(id);
        if(Student==null) {
            result.error500("未找到对应实体");
        }else {
            //update-begin--Author:huangzhilin  Date:20140417 for：[bugfree号]数据字典增加级联删除功能--------------------
            boolean ok = iStudentService.removeById(id);
            //update-begin--Author:huangzhilin  Date:20140417 for：[bugfree号]数据字典增加级联删除功能--------------------
            if(ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }


    /**
     * @功能：编辑
     * @param student
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<Student> edit(@RequestBody Student student) {
            Result<Student> result = new Result<Student>();
        Student Student1 = iStudentService.getById(student.getId());
        if(Student1==null) {
            result.error500("未找到对应实体");
        }else {
            student.setUpdateTime(new Date());
            boolean ok = iStudentService.updateById(student);
            //TODO 返回false说明什么？
            if(ok) {
                result.success("编辑成功!");
            }
        }
        return result;
    }



    /**
     * @功能：新增
     * @param student
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<Student> add(@RequestBody Student student) {
        Result<Student> result = new Result<Student>();
        try {
            student.setCreateTime(new Date());
            iStudentService.save(student);
            result.success("保存成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }
}
